/**
 *
 */
package name.yzhu.httpserver;

import java.util.Map.Entry;

import name.yzhu.common.CommonConstant;
import name.yzhu.common.yaml.YamlItem;

/**
 * @author yhzhu
 *
 */
public final class HttpServerUtil {

	static public  String format(String title, String body) {
		StringBuilder sb = new StringBuilder();
		sb.append("<html>\n");
		sb.append("<head>\n");
		sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n");
		sb.append("<meta name=\"citation_author\" content=\"Yinghu Zhu\"/>\n");
		if (title != null)
		    sb.append("<title>"+title+"</title>");
		sb.append("</head>\n");
		sb.append("<body>\n");
		sb.append(body);
		sb.append("</body>\n");
		sb.append("</html>");

		return sb.toString();
	}
	
	static public String yamlToHtml(final YamlItem yaml) {
		StringBuilder sb = new StringBuilder();
		sb.append(CommonConstant.YAML_ITEM_BEGIN_STR);
		sb.append("<br/>");
		String text;
		boolean flag = false;
		for(Entry<String, Object>  e: yaml.entrySet()) {
			if (e.getValue() != null) {
			   String v = String.valueOf(e.getValue());
			   sb.append("<b>");
			   sb.append(e.getKey());
			   sb.append("</b>");
			   sb.append(": ");
			   sb.append(v);
			   sb.append("<br/>");
			   flag = true;
			}
		}
		if (flag){
			sb.append(CommonConstant.YAML_ITEM_END_STR);
			sb.append("<br/>");
			text = sb.toString();
			return text;
		}
		return null;
	}
}
